<template>
	<view class="date-range-picker">
		<picker mode="date" :value="selectSartDate" :start="startDate" :end="endDate" @change="bindDateChange($event, 'selectSartDate')">
			<view class="input">{{selectSartDate}}</view>
		</picker>
		<text>至</text>
		<picker mode="date" :value="selectEndDate" :start="startDate" :end="endDate" @change="bindDateChange($event, 'selectEndDate')">
			<view class="input">{{selectEndDate}}</view>
		</picker>
	</view>
</template>

<script>
	import { getDate } from '@/common/utils/date.js'
	export default {
		name: 'date-range-picker',
		data () {
			const currentDate = this.getDate({
				format: true
			})
			return {
				dateFormat: '',
				selectSartDate: this.getDate(null, new Date().getFullYear() - 3),
				selectEndDate: currentDate
			}
		},
		 computed: {
			startDate() {
				return this.getDate('start');
			},
			endDate() {
				return this.getDate('end');
			}
		},
		watch: {
			selectSartDate(val) {				
				this.$emit('change', {
					startDate: this.selectSartDate,
					endDate: this.selectEndDate
				})
			},
			selectEndDate(val) {
				this.$emit('change', {
					startDate: this.selectSartDate,
					endDate: this.selectEndDate
				})
			}
		},
		methods: {
			bindDateChange(ev, key) {
				 this[key] = ev.target.value
			},
			 getDate(type, yearN) {
				const date = new Date();
				let year = date.getFullYear();
				if (yearN) {
					year = yearN
				}
				let month = date.getMonth() + 1;
				let day = date.getDate();

				if (type === 'start') {
					year = year - 60;
				} else if (type === 'end') {
					year = year
				}
				month = month > 9 ? month : '0' + month;;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			}
		}
	}
</script>

<style lang="scss" >
.date-range-picker {
	display: flex;
	justify-content: space-around;	
	height: 60upx;
	line-height: 60upx;
	.input {
		padding: 0 30upx;
		background: #f4f4f4;
		flex: 1;
	}
}

</style>
